#include "head_client.h"
#define LOG(msg) log_message(__FUNCTION__, __FILE__, __LINE__, msg)

void log_message(const char *function, const char *file, int line, const char *message) {
    openlog("三组的百度网盘项目", LOG_CONS | LOG_PID, LOG_USER);
    time_t now = time(NULL);
    char timestr[64];
    strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now));
    syslog(LOG_INFO, "[%s] %s:%d %s(): %s", timestr, file, line, function, message);
    closelog();
}
#define LOG(msg) log_message(__FUNCTION__, __FILE__, __LINE__, msg)
int flag_exit_by_client =0;

int main(int argc, char *argv[])
{   
    LOG("客户端登录");
    // ./client 192.168.118.128 1234
    ARGS_CHECK(argc, 3);
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr; // 服务端地址
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
    ERROR_CHECK(ret, -1, "connect");

    // recvFile(sockfd);

    train_t train;//发送用
    memset(&train, 0, sizeof(train_t));
    char buf[1024] = {0};

    train_t train2;//接收用

    int epfd = epoll_create(1);
    epollAdd(epfd, sockfd);
    epollAdd(epfd, STDIN_FILENO);
    //printf("/home/client1$ :\n");
    // fgets(buf, 1023, stdin);

    char commandSave[500];//客户端接收命令用

    while (1)
    {
        printf("/home/client1$ :\n");
        struct epoll_event readySet[10];
        int readyNum = epoll_wait(epfd, readySet, 10, -1);
      

        for (int i = 0; i < readyNum; ++i)
        {
            if (readySet[i].data.fd == sockfd)
            {
                // 客户端收到回复
                //char commandSave[500];
                //bzero(&commandSave,sizeof(commandSave));
                //收到服务端发的指令 
                bzero(&train2,sizeof(train2));
                recvn(sockfd,&train2.length,sizeof(train2.length));
                recvn(sockfd,train2.data,train2.length);
                //printf("command = %s\n",train2.data);
                commandSelect(train2.data, "char *path",sockfd,"char* fileNameToUser");

            }
            else if (readySet[i].data.fd == STDIN_FILENO)
            {
                // 客户端发送命令
                bzero(buf,sizeof(buf));
                bzero(&train, sizeof(train));
                ssize_t ret =read(STDIN_FILENO,buf,sizeof(buf));
                //printf("ret = %ld\n",ret);
                //printf("strlen(buf) = %ld\n",strlen(buf));
                buf[strlen(buf) - 1] = '\0';
                //printf("strlen(buf) = %ld\n",strlen(buf));
                train.length = strlen(buf);
                memcpy(train.data, buf, train.length);
                send(sockfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
                
            }
            if(flag_exit_by_client ==1)
            {
                goto end;
            }
            printf("\n--------------------------------------------------\n");
        }
    }
end: 
    close(sockfd);
    close(STDIN_FILENO);
    return 0;
}